#coding=utf-8
#Boa:Dialog:DlgCategory

import wx
from MyListCtrl import MyCategoryTreeCtrl
from myLogger import *
from product import CDataCategory

def create(parent):
    return DlgCategory(parent)

[wxID_DLGCATEGORY, wxID_DLGCATEGORYBTNCANCEL, wxID_DLGCATEGORYBTNOK, 
 wxID_DLGCATEGORYBTNUNCHECK, 
] = [wx.NewId() for _init_ctrls in range(4)]

class DlgCategory(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGCATEGORY, name=u'DlgCategory',
              parent=prnt, pos=wx.Point(532, 193), size=wx.Size(452, 500),
              style=wx.SIMPLE_BORDER, title=u'\u5546\u54c1\u7c7b\u522b')
        self.SetClientSize(wx.Size(444, 439))

        self.btnOK = wx.Button(id=wxID_DLGCATEGORYBTNOK, label=u'\u786e\u5b9a',
              name=u'btnOK', parent=self, pos=wx.Point(344, 176),
              size=wx.Size(75, 24), style=0)
        self.btnOK.Bind(wx.EVT_BUTTON, self.OnBtnOKButton,
              id=wxID_DLGCATEGORYBTNOK)

        self.btnCancel = wx.Button(id=wxID_DLGCATEGORYBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(344, 224), size=wx.Size(75, 24), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGCATEGORYBTNCANCEL)

        self.btnUncheck = wx.Button(id=wxID_DLGCATEGORYBTNUNCHECK,
              label=u'\u53d6\u6d88\u9009\u4e2d', name=u'btnUncheck',
              parent=self, pos=wx.Point(344, 128), size=wx.Size(75, 24),
              style=0)
        self.btnUncheck.Bind(wx.EVT_BUTTON, self.OnBtnUncheckButton,
              id=wxID_DLGCATEGORYBTNUNCHECK)

    def __init__(self, parent):
        self._init_ctrls(parent)

        self.myInit()

    def myInit(self):
        self.category = MyCategoryTreeCtrl(self)
        self.category.SetSize(wx.Size(300, 400))
        self.category.Move(wx.Point(10, 10))
        self.category.Bind(wx.EVT_LEFT_DCLICK, self.OnTreeLeftDClick)
        self.category.SetFocus()
        
    def setSelection(self, categoryObj):
        self.category.setSelection(categoryObj)
        
    def OnTreeLeftDClick(self, event):
        pt = event.GetPosition();
        item, flags = self.category.HitTest(pt)
        if item:
            self.categorySel = self.category.getSelection()
            self.EndModal(wx.ID_OK)
            
    def OnBtnOKButton(self, event):
        self.categorySel = self.category.getSelection()
##        if (self.categorySel is None):
##            dlg = wx.MessageDialog(self, u'没有选择分类', u'', wx.OK|wx.ICON_ERROR)
##            dlg.ShowModal()
##            return
        #log(LOG_DBG, 'sel %s', self.categorySel)
        self.EndModal(wx.ID_OK)

    def OnBtnCancelButton(self, event):
        self.EndModal(wx.ID_CANCEL)

    def OnBtnUncheckButton(self, event):
        '''取消选中项目'''
        self.categorySel = None
        self.category.UnselectAll()
